import { login, getInfo, register, update, updatePassword } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    id: '',
    phone: '',
    address: '',
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, sellerName) => {
    state.name = sellerName
  },
  SET_ID: (state, sellerId) => {
    state.id = sellerId;
  },
  SET_PHONE: (state, sellerPhone) => {
    state.phone = sellerPhone;
  },
  SET_ADDRESS: (state, sellerAddress) => {
    state.address = sellerAddress;
  },
}

const actions = {
  // 用户登录
  async login({ commit }, userInfo) {
    const { username, password } = userInfo;
    let result = await login({ sellerId: username.trim(), sellerPw: password });
    if (result.code == 200) {
      if (result.data) {
        commit('SET_TOKEN', result.data);
        setToken(result.data);
        return 'ok';
      } else {
        alert('账号或密码错误');
      }
    } else {
      return Promise.reject(new Error('false'));
    }
  },
  // 用户注册
  async register({ commit }, userInfo) {
    const { username, password,phone ,address } = userInfo;
    let result = await register({ sellerName: username.trim(),sellerId: username.trim(), sellerPw: password ,sellerPhone: phone ,sellerAddress: address});
    if (result.code == 200) {
      return result.data
    } else {
      return Promise.reject(new Error('false'));
    }
  },
// 用户信息修改
async update({ commit }, userInfo) {
  const { username, id, phone ,address } = userInfo;
  let result = await update({ sellerName: username.trim(), sellerId: id,sellerPhone: phone ,sellerAddress: address });
  if (result.code == 200) {
    this.getInfo();
    return 'ok'
  } else {
    return Promise.reject(new Error('false'));
  }
},
 // 用户密码修改
 async updatePassword({ commit }, passwordForm) {
  const { oldPassword, password } = passwordForm;
  let result = await updatePassword(oldPassword, password);
  if (result.code == 200) {
    return result.data
  } else {
    return Promise.reject(new Error('false'));
  }
},
  // 获取用户信息
  async getInfo({ commit, state }) {
    let result = await getInfo(state.token);
    if (result.code == 200) {

      if (!result.data) {
        return reject('Verification failed, please Login again.')
      }
      const { sellerName, sellerId, sellerPhone, sellerAddress } = result.data;
      commit('SET_NAME', sellerName);
      commit('SET_ID', sellerId);
      commit('SET_PHONE', sellerPhone);
      commit('SET_ADDRESS', sellerAddress);
      return 'ok';
    } else {
      return Promise.reject(new Error('false'));
    }
  },

  // 用户退出登录
  logout({ commit, state }) {
    removeToken() // must remove  token  first
    resetRouter()
    commit('RESET_STATE')
  },

  // 移除token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

